<?php

    // +----------------------------------------------------------------------
    // | 站内消息
    // +----------------------------------------------------------------------
    // | Copyright (c) 2015-2024 http://www.yicmf.com, All rights reserved.
    // +----------------------------------------------------------------------
    // | Author: 微尘 <yicmf@qq.com>
    // +----------------------------------------------------------------------

    namespace app\controller\ucenter;

    use yicmf\builder\Edit;
    use yicmf\builder\Table;
    use app\event\ucenter\Notice as NoticeEvent;
    use app\event\ucenter\User as UserEvent;
    use app\model\ucenter\Message as MessageModel;
    use app\model\ucenter\UserGrade as UserGradeModel;

    class  Message extends Common
    {
        protected function initialize()
        {
            parent::initialize();
        }


        public function _filter()
        {
            $map = [];
            $map[] = ['status', '>', -2];
            $map[] = ['user_id', '=', $this->user['id']];
            return $map;
        }

		public function test()
		{
			dump($this->user);
		}
        public function index()
        {
            // 读取规则列表
            // 显示页面
            $statusOptions = [
                [
                    'id' => 1,
                    'value' => '已读',
                ],
                [
                    'id' => 2,
                    'value' => '未读',
                ]
            ];
            // 显示页面
            $builder = new Table();
            $builder->model(MessageModel::class)
                ->where($this->_filter())
                ->order('id DESC')
                ->searchText('content', '通知内容：')
                ->searchSelect('status', '状态：', $statusOptions)
                ->keyId()
                ->keyTitle('标题', false, '200')
                ->keyText('content', '详细', false, '404')
                ->keySwitch('is_read', '读取', '已读|未读', false, 95)
                ->keyUser('user_id', '收件人')
                ->keyUser('from_user_id', '发件人')
                ->keyIp()
                ->keyCreateTime()
                ->actionToCheck('read?id={$id}', '已读',function ($data){
                	return !$data['is_read'];
				})
                ->actionDelete('del?id={$id}', '删除');
            return $builder->fetch();
        }

        public function admin()
        {
            // 读取规则列表
            // 显示页面
            $statusOptions = [
                [
                    'id' => 1,
                    'value' => '已读',
                ],
                [
                    'id' => 2,
                    'value' => '未读',
                ]
            ];
            $map = [];
            $map[] = ['status', '>', -2];
            $map[] = ['user_id', '=', $this->user['id']];
            // 显示页面
            $builder = new Table();
            $builder->model(MessageModel::class)
                ->where($map)
                ->order('id DESC')
                ->searchText('content', '通知内容：')
                ->searchSelect('status', '状态：', $statusOptions)
                ->keyId()
                ->keyTitle('标题', false, '200')
                ->keyText('content', '详细', false, '404')
                ->keySwitch('is_read', '读取', '已读|未读', false, 92)
                ->keyUser('user_id', '收件人')
                ->keyUser('from_user_id', '发件人')
                ->keyIp()
                ->keyCreateTime()
                ->actionView('read?id={$id}', '查看')
                ->actionDelete('del?id={$id}', '删除');
            return $builder->fetch();
        }

        /**
         * 新增消息
         */
        public function add()
        {
            if ($this->request->isPost()) {
                $result = NoticeEvent::sendByAdmin($this->request->post(), $this->user, $this->request->post('type', 0));
                if (0 == $result['code']) {
                    $this->_returnSuccess( $result['message'], true);
                } else {
                    $this->_returnError( '发送消息失败，失败信息【' . $result['message'] . '】');
                }
            } else {
                //判断是否为编辑模式
                $isEdit = $this->request->has('id', 'get') ? true : false;
                if ($isEdit) {
                    $data = $this->model->find($this->request->get('id'));
                } else {
                    $data = [];
                    $data['status'] = 1;
                    $data['type'] = $this->request->get('type');
                }
                $builder = new Edit();
                $builder->title('发送消息')
                    ->keyText('title', '短消息标题')
                    ->keyTextArea('content', '短消息内容')
                    ->keyRadio('type', '发送方式', ['单个', '用户组', '金牌卖家', '全部'])
                    ->keyRadio('user_grade_id', '用户组', UserGradeModel::where('status', 1)->column('title', 'id'), '', 1)
                    ->keyNumber('user_id', '用户ID')
                    ->keyNumber('user_page', '用户分页', '单次发送用户数最多10000个，超过则分页发送', 1)
                    ->setTrigger('type', 0, 'user_id')
                    ->setTrigger('type', 1, 'user_grade_id,user_page')
                    ->setTrigger('type', 2, 'user_page')
                    ->setTrigger('type', 3, 'user_page')
                    ->data($data)
                    ->buttonSubmit('', '保存')->buttonClose();
                return $builder->fetch();
            }
        }

        /**
         * 设置已读
         * @throws \think\Exception
         * @author  : 微尘 <yicmf@qq.com>
         * @datetime: 2019/5/8 16:49
         */
        public function read()
        {
            $message = $this->user->messages()->where('id', $this->request->param('id'))->find();
            if ($message) {
                $message['is_read'] = 1;
                $message->save();
                $this->_returnSuccess( '操作成功');
            } else {
                $this->_returnError( '留言不存在');
            }
        }

        /**
         * 删除消息
         */
        public function del()
        {
            $message = MessageModel::where('id', $this->request->param('id'))->find();
            if ($message) {
                $message['status'] = -2;
                $message->save();
                $this->_returnSuccess( '删除成功');
            } else {
                $this->_returnSuccess( '操作失败');
            }
        }


    }
